﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Boos : MonoBehaviour {
    [Header("最大血量")]
    public float maxBlood;

    [Header("当前血量")]
    public float Blood;

    [Header("血量视图")]
    public EnemyBloodController bloodView;

    private Animator an;
    private Rigidbody2D rg;
    private CapsuleCollider2D ca;

    public Transform player;

    public bool isFlipped = false;

    public void LookAtPlayer()
    {
        Vector3 flipped = transform.localScale;
        flipped.z *= -1f;

        if (transform.position.x > player.position.x && isFlipped)
        {
            transform.localScale = flipped;
            transform.Rotate(0f, 180f, 0f);
            isFlipped = false;
        }
        else if (transform.position.x <player.position.x&&!isFlipped)
        {
            transform.localScale = flipped;
            transform.Rotate(0f, 180f, 0f);
            isFlipped = true;
        }
    }

    public Transform Cpoint;
    private bool Collid;
    public LayerMask layer;
    public void Attack()
    {
        Collid = Physics2D.OverlapCircle(Cpoint.position, 6, layer);

        if (Collid)
        {
            PlayerBloodController.con.SetBlood();
            PlayerController.con.PlayerHurt();
        }
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "sword")
        {
            Blood = Blood - Player.con.attack;
            bloodView.SetBloodValue(Blood);
            if (Blood<=222)
            {
                GetComponent<Animator>().SetBool("IsEnrage", true);
            }
            if (Blood <= 0)
            {
                Destroy(gameObject);
                GameController.con.CreatMen();
                CoinController.con.CreatCoin(Cpoint, 100);
            }
        }
    }
    // Use this for initialization
    void Start () {

        Blood = maxBlood;
        bloodView.maxBlood = maxBlood;
        rg = GetComponent<Rigidbody2D>();
        ca = GetComponent<CapsuleCollider2D>();
        an = GetComponent<Animator>();
    }
	
	// Update is called once per frame
	void Update () {
    }

    public void jian()
    {
        GameController.con.Njiasword.SetActive(true);
    }
}
